package rgbos.forum.webClient;

import java.util.Map;

import javax.security.auth.login.LoginException;

import rgbos.forum.domain.Auditor;
import rgbos.forum.domain.User;
import rgbos.forum.exceptions.MailException;

import jaxcent.HtmlElement;
import jaxcent.HtmlImage;
import jaxcent.HtmlInputSubmit;
import jaxcent.HtmlPara;
import jaxcent.HtmlTable;
import jaxcent.JaxcentPage;
import jaxcent.Jaxception;


public class LogIn extends jaxcent.JaxcentPage {	
//		HtmlPara para = new HtmlPara(this,"header");
		private boolean flag = false;
		private WebClientHandler handler_ = WebClientHandler.getWebClientHandler();
		private HtmlImage loading = new HtmlImage(this, "loading");
		private HtmlInputSubmit submitButton = new HtmlInputSubmit(this, "logInButton") {

			protected void onClick( Map pageData ) {			
				String tUsername = (String) pageData.get("uName");
				String tPassword = (String) pageData.get("pass");
				try {				
					Map sessionData = getAllSessionData( false );				
					WebClientHandler clientHandler = WebClientHandler.getWebClientHandler();
					String permissions = clientHandler.login(tUsername, tPassword);
					Auditor.Log("User " + tUsername + " logged in from IP:"+getRemoteAddr(),this.getClass());				
					sessionData.put("permissions", permissions);
					WebClientHandler handler = WebClientHandler.getWebClientHandler();				
					navigate( "/showTopics.html" );
				} catch (LoginException e) {
					showMessageDialog("Username or password not correct");
					Auditor.Log("User " + tUsername + " couldn't log in because the user or pass was not correct",this.getClass());				
				} catch (Jaxception e) {
				} catch (Exception e) {
				}
			}
		};
		
		private HtmlInputSubmit forgotPasswordButton = new HtmlInputSubmit(this, "passwordButton") {

			protected void onClick( Map pageData ) {
				if (flag) {
					showMessageDialog("Please press only once!");
					return;
				}
				flag = true;
				String tUsername = (String) pageData.get("passwordUsername");				
				String tEmail = (String) pageData.get("passwordEmail");
				if (tUsername == null || tUsername.equals("") || tEmail == null || tEmail.equals("")) {
					showMessageDialog("Please fill both username and email!");
					return;
				}
				try {	
					loading.setStyle("visibility", "visible");
					User user = handler_.getControl().getUserByName(tUsername);
					if (!user.getEmail().equals(tEmail)) {
						showMessageDialog("Email is not correct");
						loading.setStyle("visibility", "hidden");
						return;
					}
					Auditor.Log("User " + tUsername + " requested a new password.",this.getClass());				
					handler_.forgotPassword(user);		
					loading.setStyle("visibility", "hidden");
					showMessageDialog("A new password was sent to your email. Please use it for your next login attempt");					
				} catch (MailException e) {
					Auditor.Log(e.getMessage(),this.getClass());
					showMessageDialog("Can't email new password at the moment. Please try again later");
					try {
						loading.setStyle("visibility", "hidden");
					} catch (Jaxception e1) {
						e1.printStackTrace();
					}
				} catch (Jaxception e) {
					e.printStackTrace();
				} finally {
					flag = true;
				}
				flag = true;
			}
		};
		
		private final JaxcentPage page = this;
		
		HtmlElement forgotPassword = new HtmlElement(this, "forgotPassword") {
			protected void onClick( Map pageData ) {
				HtmlTable forgotPasswordTable = new HtmlTable(page, "passwordTable");
				try {
					if (forgotPasswordTable.getStyle("display").contentEquals("none")) {								
						forgotPasswordTable.setStyle("display", "block");
					}
					else {
						forgotPasswordTable.setStyle("display", "none");
					}
				} catch (Jaxception e) {
					e.printStackTrace();
				}
			}
		};
	
	public LogIn() {
		super();	
	}
	
	protected void onLoad() {
		try{
//			setFormSaveEnabled(false);
//			Map sessionData1 = getAllSessionData(false);
//			handler_.DisplayHeader(para, sessionData1, this);
			forgotPasswordButton.setValue("Request new Password");
			submitButton.setValue("Login");
			
		} catch (Jaxception e) {
			e.printStackTrace();
		}				
	}

}
